layui.use(['layer','jquery'], function(){
	var layer = layui.layer
	,$ = layui.jquery;
	var id = $('#totalpic').attr('data-id');
	var picTotal = $('#pic_total').text();
	var picList;
	$.ajax({
		async: false,
  	  	url: '/index/detailImg.html?id='+id,
  	  	type: 'POST',
  	  	dataType: 'json',
  	  	timeout: 1000,
  	  	beforeSend: function(){
			var index = layer.load(1, {
			shade: [0.1,'#fff'],
			time: 1000
			});
  	  	},
  	  	error: function(){
  	  		layer.alert('无法获取数据，请稍后重试！', {
				skin: 'layui-alert',
				anim: 0  	  			
  	  		});
  	  	},
  	  	complete: function(){
  	  		layer.close(layer.index); 
  	  	},success: function(json){
  	  		picList = json;
  	  	}
	});
	$('#prepic').on('click', function(){
		var curkey = $('#show_cur_img').find('img').attr('data-key');
		if(curkey == 0){
			layer.alert('当前已经是第一张图片了！', {
				skin: 'layui-alert',
				anim: 0
			});	
		}else{
			prekey = parseInt(curkey) - 1;
			$('#show_cur_img').html('<img src="'+picList[prekey].url+'" alt="'+picList[prekey].alt+'" data-key="'+prekey+'" />');
			$('#cur_no').text(prekey +1);			
		}
	});
	$('#nextpic,#show_cur_img').on('click', function(){
		var curkey = $('#show_cur_img').find('img').attr('data-key');
		if(curkey == parseInt(picTotal) -1){
			layer.alert('当前已经是最后一张图片了！', {
				skin: 'layui-alert',
				anim: 0
			});				
		}else{
			nextkey = parseInt(curkey) + 1;
			$('#show_cur_img').html('<img src="'+picList[nextkey].url+'" alt="'+picList[nextkey].alt+'" data-key="'+nextkey+'" />');
			$('#cur_no').text(nextkey +1);
		}
	});
	$('.layui-bigpic').on('click', function(){
		var imgurl = $('#show_cur_img').find('img').attr('src');
		window.open(imgurl);
	});
	$('.layui-enjoy').on('click', function(){
		var enjoy_count = $('#enjoy_count').text();
		var saveid = GetCookie('enjoyid');
		if(saveid == id){
			layer.alert('您已经提交过了！', {
				skin: 'layui-alert'
			});
		}else{
			$.post('/index/addEnjoy.html?id='+id, function(str){
				layer.alert(str, {
					skin: 'layui-alert'
				});
				$('#enjoy_count').text(parseInt(enjoy_count)+1);
			});
			SetCookie('enjoyid',id,1);
		}
	});
/*	$('.layui-rec-title a').on('click', function(){
		var index = $(this).index(this);
		$(this).removeClass('on').eq(index).addClass('on');
		$('.layui-rec-box').hide().eq(index).show();
	});*/
});